home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / aevt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-17  |  5.0 KB  |  196 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     aevt.c
  4.  
  5.     This module handles Apple events.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <stdio.h>
  13. #include <appleevents.h>
  14.  
  15. #include "glob.h"
  16. #include "aevt.h"
  17. #include "dlgutil.h"
  18. #include "newsrc.h"
  19. #include "util.h"
  20.  
  21.  
  22.  
  23. static FSSpec **gDocList = nil;        /* initial ODOC list */
  24. static short gNumDocs = 0;            /* number of document in initial ODOC list */
  25. static Boolean gStartingUp=true;    /* true during startup */
  26.  
  27.  
  28.  
  29. /*----------------------------------------------------------------------------
  30.     OpenDocList 
  31.     
  32.     Opens a list of documents.
  33.     
  34.     Entry:    docList = handle to array of FSSpec records.
  35.             numDocs = number of FSSpec records in docList.
  36.             
  37.     Exit:    Documents opened.
  38.             docList disposed.
  39. ----------------------------------------------------------------------------*/
  40.  
  41. void OpenDocList (FSSpec **docList, short numDocs)
  42. {
  43.     short i;
  44.     FSSpec theFile;
  45.     
  46.     for (i = 0; i < numDocs; i++) {
  47.         theFile = (*docList)[i];
  48.         OpenFile(&theFile);
  49.     }
  50.     MyDisposHandle((Handle)docList);
  51. }
  52.  
  53.  
  54.  
  55. /*----------------------------------------------------------------------------
  56.     HandleOAPP 
  57.     
  58.     Handles the open application event. Does nothing.
  59. ----------------------------------------------------------------------------*/
  60.  
  61. static pascal OSErr HandleOAPP (AppleEvent *event, AppleEvent *reply, long refcon)
  62. {
  63.     return noErr;
  64. }
  65.  
  66.  
  67.  
  68. /*----------------------------------------------------------------------------
  69.     HandleODOC 
  70.     
  71.     Handles the open document event.
  72.     
  73.     The initial ODOC event during startup, if any, is treated specially.
  74.     For this event, the FSSpec list is parsed and saved in the gDocList
  75.     array, but the documents are not opened. The initialization code in
  76.     init.c takes care of opening these initial documents.
  77. ----------------------------------------------------------------------------*/
  78.  
  79. static pascal OSErr HandleODOC (AppleEvent *event, AppleEvent *reply, long refcon)
  80. {
  81.     OSErr err;
  82.     AEDescList docList;
  83.     long numItems, i;
  84.     AEKeyword keywd;
  85.     DescType returnedType;
  86.     Size actualSize;
  87.     FSSpec theFile;
  88.     
  89.     if (!gStartupOK && !gStartingUp) return noErr;
  90.     err = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList);
  91.     if (err != noErr) goto exit;
  92.     err = AECountItems(&docList, &numItems);
  93.     if (err != noErr) goto exit;
  94.     gDocList = (FSSpec**)MyNewHandle(numItems * sizeof(FSSpec));
  95.     for (i = 1; i <= numItems; i++) {
  96.         err = AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
  97.             (Ptr)&theFile, sizeof(theFile), &actualSize);
  98.         if (err != noErr) goto exit;
  99.         (*gDocList)[i-1] = theFile;
  100.     }
  101.     err = AEDisposeDesc(&docList);
  102.     if (err != noErr) goto exit;
  103.     gNumDocs = numItems;
  104.     if (gStartingUp) return noErr;
  105.     OpenDocList(gDocList, gNumDocs);
  106.     return noErr;
  107.  
  108. exit:
  109.     UnexpectedErrorMessage(err);
  110.     return err;
  111. }
  112.  
  113.  
  114.  
  115. /*----------------------------------------------------------------------------
  116.     HandlePDOC 
  117.     
  118.     Handles the print document event. Does nothing.
  119. ----------------------------------------------------------------------------*/
  120.  
  121. static pascal OSErr HandlePDOC (AppleEvent *event, AppleEvent *reply, long refcon)
  122. {
  123.     return noErr;
  124. }
  125.  
  126.  
  127.  
  128. /*----------------------------------------------------------------------------
  129.     HandleQUIT 
  130.     
  131.     Handles the quit application event.
  132. ----------------------------------------------------------------------------*/
  133.  
  134. static pascal OSErr HandleQUIT (AppleEvent *event, AppleEvent *reply, long refcon)
  135. {
  136.     gDone = true;
  137.     return noErr;
  138. }
  139.  
  140.  
  141.  
  142. /*----------------------------------------------------------------------------
  143.     InitializeAppleEvents 
  144.     
  145.     Initializes Apple events. Installs the handlers.
  146. ----------------------------------------------------------------------------*/
  147.  
  148. void InitializeAppleEvents (void)
  149. {
  150.     OSErr err;
  151.  
  152.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  153.         HandleOAPP, 0, false);
  154.     if (err) goto exit;
  155.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  156.         HandleODOC, 0, false);
  157.     if (err) goto exit;
  158.     err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  159.         HandlePDOC, 0, false);
  160.     if (err) goto exit;
  161.     err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  162.         HandleQUIT, 0, false);
  163.     if (err) goto exit;
  164.     return;
  165.     
  166. exit:
  167.     ErrorMessage("Internal error - could not install Apple event handlers.");
  168.     ExitToShell();
  169. }
  170.  
  171.  
  172.  
  173. /*----------------------------------------------------------------------------
  174.     GetInitialDocList 
  175.     
  176.     Gets the initial document list from the first ODOC or OAPP event.
  177.     
  178.     Exit:    *docList = handle to array of FSSpec records.
  179.             *numDocs = number of FSSpec records in docList.
  180. ----------------------------------------------------------------------------*/
  181.  
  182. void GetInitialDocList (FSSpec ***docList, short *numDocs)
  183. {
  184.     EventRecord ev;
  185.     Boolean gotEvt;
  186.  
  187.     while (gStartingUp) {
  188.         gotEvt = WaitNextEvent(highLevelEventMask, &ev, GetCaretTime(), nil);
  189.         if (gotEvt && ev.what == kHighLevelEvent) {
  190.             AEProcessAppleEvent(&ev);
  191.             gStartingUp = false;
  192.         }
  193.     }
  194.     *docList = gDocList;
  195.     *numDocs = gNumDocs;
  196. }